package sg.edu.nus.iss.team2s.jass.util;


import java.awt.*;
import java.awt.event.*;

/**
 * @author LU ZHIXIAN
 */

public abstract class OkCancelDialog extends Dialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7741513916488554986L;
	private String okText;
	private String cancelText;


	public OkCancelDialog (Frame parent, String title, String okText, String cancelText) {
		super (parent, title);

		this.okText = okText;
		this.cancelText = cancelText;        	
		add ("Center", createFormPanel());
		add ("South",  createButtonPanel());

		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent we){
				destroyDialog();
			}
		});
	}    

	public OkCancelDialog (Frame parent, String title) {
		this(parent, title, "OK", "Cancel");
	}    

	public OkCancelDialog (Frame parent) {
		this (parent, "");
	}

	private Panel createButtonPanel () {
		Panel p = new Panel ();

		Button b;
		ActionListener l;

		b = new Button (this.okText);
		b.setSize(100, b.getHeight()); 
		l = new ActionListener () {
			public void actionPerformed (ActionEvent e) {
				boolean success = performOkAction ();
				if (success) {
					destroyDialog ();
				}
			}
		};
		b.addActionListener (l);
		if (b.getLabel() == "")
			b.setVisible(false);
		else
			p.add (b);

		b = new Button(this.cancelText);
		b.setSize(100, b.getHeight());
		l = new ActionListener () {
			public void actionPerformed (ActionEvent e) {                
				if (performCancelAction())
					destroyDialog ();
			}
		};
		b.addActionListener (l);
		if (b.getLabel() == "")
			b.setVisible(false);
		else
			p.add (b);

		return p;
	}

	private void destroyDialog () {
		setVisible (false);
		dispose();
	}

	protected abstract Panel createFormPanel () ;

	protected abstract boolean performOkAction () ;

	protected boolean performCancelAction () {return true;};   
}